package com.itheima.a01mystream;

import java.util.*;
import java.util.stream.Collectors;

public class StreamDemo4 {
    public static void main(String[] args) {
        //stream流终结方法

        //创建集合
        //需要过滤的数据
        ArrayList<String> list = new ArrayList<String>();
        Collections.addAll(list, "A1-1","A-2", "AB-3", "C-4", "AD-5", "AAE-6", "F-7", "G-8");

        //1.forEach 遍历
        list.stream().forEach(s->System.out.println(s));

        //2.count 统计
        System.out.println("=================");
        long count = list.stream().count();
        System.out.println(count);

        //toArray() 收集流中的数据，放到数组,可以指定数组类型
        System.out.println("=================");
        Object[] array = list.stream().toArray(value->new String[value]);
        System.out.println(Arrays.toString(array));

        //3.collect 收集
        //3.1 用list收集
        System.out.println("=================");
        List<String> collect = list.stream().collect(Collectors.toList());
        System.out.println(collect);
        //3.2 用set收集
        System.out.println("=================");
        Set<String> collect1 = list.stream().collect(Collectors.toSet());
        System.out.println(collect1);
        //3.3 用map收集
        System.out.println("=================");
        Map<String, Integer> collect2 = list.stream()
                .collect(Collectors.toMap(s->s.split("-")[0]
                        ,s->Integer.parseInt(s.split("-")[1])));
        System.out.println(collect2);


    }
}
